import Vue from 'vue';
import Vuex from 'vuex';
import VueCookies from 'vue-cookies';
import tool from "ut/tool";
import router from "../router";
Vue.use(Vuex);

// 用户
const User = {
    namespaced: true,
    actions: {
        // 登录
        login(con, e) {
            tool.request('/user/login', e).then(res => {
                let { code, msg, data } = res.data;
                switch (code) {
                    case 200:
                        data.role = e.role;
                        con.commit('SaveUserInfo', data);
                        VueCookies.set('userInfo', data);
                        tool.notif('success', '成功', '登录成功');
                        router.replace('/index');
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 获取用户信息
        getUserInfo(con, type = 0) {
            let userInfo = VueCookies.get('userInfo');
            if (type) {
                let { role, id } = userInfo;
                tool.request('/user/getUserInfo', { role, id }).then(res => {
                    let { code, msg, data } = res.data;
                    switch (code) {
                        case 200:
                            data.role = role;
                            con.commit('SaveUserInfo', data);
                            VueCookies.set('userInfo', data);
                            break;
                        default: tool.notif('error', '错误', msg); break;
                    }
                })
            } else con.commit('SaveUserInfo', userInfo);
        },
        // 修改用户名
        editName(con,name) {
            let { id,role } = VueCookies.get('userInfo');
            tool.request('/user/editName',{ id,role,name }).then(res => {
                let { code,msg } = res.data;
                if (code == 200) {
                    con.dispatch('getUserInfo');
                    tool.notif('success','成功','修改成功');
                } else tool.notif('error','错误',msg);
            })
        },
        // 修改手机号
        editAccount(con,account) {
            let { id,role } = VueCookies.get('userInfo');
            tool.request('/user/editAccount',{ id,role,account }).then(res => {
                let { code,msg } = res.data;
                if (code == 200) {
                    con.dispatch('getUserInfo');
                    tool.notif('success','成功','修改成功');
                } else tool.notif('error','错误',msg);
            })
        },
        // 修改头像
        editAvatar(con,avatar) {
            let { id,role } = VueCookies.get('userInfo');
            tool.request('/user/editAvatar',{ id,role,avatar }).then(res => {
                let { code,msg } = res.data;
                if (code == 200) {
                    con.dispatch('getUserInfo');
                    tool.notif('success','成功','修改成功');
                } else tool.notif('error','错误',msg);
            })
        },
        // 重置密码
        resetPassword(con,e) {
            tool.request('/user/resetPassword',e).then(res => {
                let { code,msg } = res.data;
                if (code == 200) {
                    con.dispatch('getUserInfo');
                    tool.confirm('提示','密码重置成功，请重新登录','success',false).then(res => {
                        router.replace('/login');
                        VueCookies.remove('userInfo');
                    })
                } else tool.notif('error','错误',msg);
            })
        },
    },
    mutations: {
        SaveUserInfo(state, e) {
            state.userInfo = e;
        },
    },
    state: {
        userInfo: {}
    }
}

// 课题
const Topic = {
    namespaced: true,
    actions: {
        // 获取课题列表
        getTopicList(con, { type_id, year, page, pagesize }) {
            if (type_id && year) {
                tool.request('/topic/getList', { type_id, year, page, pagesize }).then(res => {
                    let { code, msg, data, total } = res.data;
                    switch (code) {
                        case 200: con.commit('SaveTopicList', data); break;
                        default: tool.notif('error', '错误', msg); break;
                    }
                    con.commit('SaveTotal', total);
                })
            }
        },
        // 获取课题年份
        getTopicYears(con) {
            tool.request('/topic/getYear').then(res => {
                con.commit('SaveYears', res.data.data)
            })
        },
        // 获取课题分类
        getTypes(con) {
            tool.request('/topic/getTypes').then(res => {
                con.commit('SaveTypes', res.data.data)
            })
        },
        // 添加分类
        addType(con, name) {
            tool.request('/topic/addType', { name }).then(res => {
                tool.handleResult(res, '添加成功').then(res => {
                    con.dispatch('getTypes');
                })
            })
        },
        // 修改分类
        editType(con, { id, name }) {
            tool.request('/topic/updateType', { id, name }).then(res => {
                tool.handleResult(res, '修改成功').then(res => {
                    con.dispatch('getTypes');
                })
            })
        },
        // 删除分类
        delType(con, id) {
            tool.request('/topic/delType', { id }).then(res => {
                tool.handleResult(res, '删除成功').then(res => {
                    con.dispatch('getTypes');
                })
            })
        },
        // 分页获取待审核课题
        getInspect(con, { page, pagesize }) {
            tool.request('/topic/getInspect', { page, pagesize }).then(res => {
                let { code, msg, data, total } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveTopicList', data);
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
                con.commit('SaveTotal', total);
            })
        },
        // 审核课题
        examine(con, { topic_id, state, page, pagesize }) {
            tool.confirm('提示', '此操作不可逆，是否继续？').then(res => {
                if (res) {
                    tool.request('/topic/examine', { topic_id, state }).then(res => {
                        tool.handleResult(res, '操作成功').then(res => {
                            con.dispatch('getInspect', { page, pagesize });
                        })
                    })
                }
            })
        },
        // 获取我的课题
        getMyTopic(con, e) {
            tool.request('/topic/getTeacherTopic', e).then(res => {
                let { code, msg, data, total } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveTopicList', data);
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
                con.commit('SaveTotal', total)
            })
        },
        // 删除课题
        delTopic(con, e) {
            tool.confirm('提示', '此操作不可逆，是否继续？').then(res => {
                if (res) {
                    let { id, type, page, pagesize, teacher_id } = e;
                    tool.request('/topic/delTopic', { id }).then(res => {
                        tool.handleResult(res, '操作成功').then(res => {
                            con.dispatch('getMyTopic', { type, teacher_id, page, pagesize });
                        })
                    })
                }
            })
        },
        // 发布课题
        addTopic(con, e) {
            e.teacher_id = VueCookies.get('userInfo').id;
            tool.request('/topic/addTopic', e).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.notif('success', '成功', '发布成功');
                        router.push('topic_my');
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 修改课题
        editTopic: (e) => new Promise((t, f) => {
            tool.request('/topic/updateTopic', e).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.notif('success', '成功', '修改成功');
                        t();
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        })
    },
    mutations: {
        SaveTypes(state, e) {
            state.typeList = e;
        },
        SaveTotal(state, e) {
            state.total = e;
        },
        SaveYears(state, e) {
            state.yearList = e;
        },
        SaveTopicList(state, e) {
            state.topicList = e;
        },
        SaveInspect(state, e) {

        }
    },
    state: {
        total: 0,
        typeList: [],
        yearList: [],
        topicList: []
    }
}

// 教师
const Teacher = {
    namespaced: true,
    actions: {
        // 获取教师列表
        getTeacherList(con, { page, pagesize }) {
            tool.request('/teacher/getList', { page, pagesize }).then(res => {
                let { code, msg, data, total } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveTeacherList', data);
                        con.commit('SaveTeacherTotal', total);
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 获取职务列表
        getStations(con) {
            tool.request('/teacher/getStations').then(res => {
                let { code, msg, data } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveStation', data);
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 添加职务
        addStation(con, name) {
            tool.request('/teacher/addStation', { name }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '添加成功').then(res => {
                            con.dispatch('getStations');
                        })
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 修改职务
        editStation(con, { id, name }) {
            tool.request('/teacher/editStation', { id, name }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '修改成功').then(res => {
                            con.dispatch('getStations');
                        })
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 删除职务
        delStation(con, id) {
            tool.request('/teacher/delStation', { id }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '删除成功').then(res => {
                            con.dispatch('getStations');
                        })
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 注册教师
        addTeacher(con, e) {
            tool.request('/teacher/addTeacher', e).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.notif('success', '提示', '注册成功');
                        router.replace('teacher_list');
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 注销教师
        delTeacher(con, { id, page, pagesize }) {
            tool.confirm('提示', '此操作将永久删除该教师, 是否继续?').then(res => {
                if (res) {
                    // 获取教师信息，删除图片
                    tool.request('/teacher/delTeacher', { id }).then(res => {
                        let { code, msg } = res.data;
                        switch (code) {
                            case 200:
                                tool.notif('success', '提示', '操作成功');
                                con.dispatch('getTeacherList', { page, pagesize });
                                break;
                            default: tool.notif('error', '错误', msg); break;
                        }
                    })
                }
            })
        },
        // 获取课题预约列表
        getSubscribe(con, e) {
            tool.request('/teacher/getSubscribe', e).then(res => {
                let { code, msg, data, total } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveSubscribe', data);
                        con.commit('SaveSubscribeTotal', total);
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 审核课题
        toExamine:(con, e) => new Promise((t,f) => {
            tool.request('/teacher/toExamine', e).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.notif('success', '提示', '操作成功');
                        t();
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        })
    },
    mutations: {
        SaveTeacherList(state, e) {
            state.teacherList = e;
        },
        SaveTeacherTotal(state, e) {
            state.teacherTotal = e;
        },
        SaveStation(state, e) {
            state.stations = e;
        },
        SaveSubscribe(state, e) {
            state.subscribe = e;
        },
        SaveSubscribeTotal(state, e) {
            state.subscribeTotal = e;
        },
    },
    state: {
        teacherList: [],     // 教师列表
        teacherTotal: 0,    // 教师总数量
        stations: [],       // 教师职务列表
        subscribe: [],      // 学生预约列表
        subscribeTotal: 0,  // 学生预约总数
    }
}

// 学生
const Student = {
    namespaced: true,
    actions: {
        // 获取学生列表
        getStudentList(con, { page, pagesize }) {
            tool.request('/student/getList', { page, pagesize }).then(res => {
                let { code, msg, data, total } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveList', data);
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
                con.commit('SaveTotal', total);
            })
        },
        // 获取系部列表
        getSdepts(con) {
            tool.request('/student/getSdepts').then(res => {
                let { code, msg, data } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveSdept', data);
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 添加系部
        addSdept(con, name) {
            tool.request('/student/addSdept', { name }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '添加成功').then(res => {
                            con.dispatch('getSdepts');
                        })
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 修改系部
        editSdept(con, { id, name }) {
            tool.request('/student/editSdept', { id, name }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '修改成功').then(res => {
                            con.dispatch('getSdepts');
                        })
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 删除系部
        delSdept(con, id) {
            tool.request('/student/delSdept', { id }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '删除成功').then(res => {
                            con.dispatch('getSdepts');
                        })
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 获取班级列表
        getClases(con, sdept_id) {
            tool.request('/student/getClassBySdept', { sdept_id }).then(res => {
                let { code, msg, data } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveClases', data);
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 添加班级
        addClass(con, { sdept_id, name }) {
            tool.request('/student/addClass', { sdept_id, name }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '添加成功').then(res => {
                            con.dispatch('getClases', sdept_id);
                        })
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 修改班级
        editClass(con, { sdept_id, class_id, name }) {
            tool.request('/student/editClass', { class_id, name }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '修改成功').then(res => {
                            con.dispatch('getClases', sdept_id);
                        })
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 删除班级
        delClass(con, { sdept_id, class_id }) {
            tool.request('/student/delClass', { class_id }).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.handleResult(res, '删除成功').then(res => {
                            con.dispatch('getClases', sdept_id);
                        })
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        }
    },
    mutations: {
        SaveList(state, e) {
            state.studentList = e;
        },
        SaveTotal(state, e) {
            state.studentTotal = e;
        },
        SaveSdept(state, e) {
            state.sdepts = e;
        },
        SaveClases(state, e) {
            state.clases = e;
        },
    },
    state: {
        studentList: [],
        studentTotal: 0,
        sdepts: [],
        clases: []
    }
}

// 公告
const Notice = {
    namespaced: true,
    actions: {
        // 发布
        release(con, e) {
            if (VueCookies.isKey('userInfo')) {
                let { id, role } = VueCookies.get('userInfo');
                e.type = role;
                e.sender = id;
                tool.request('/notice/release', e).then(res => {
                    let { code, msg } = res.data;
                    switch (code) {
                        case 200:
                            tool.notif('success', '成功', '发布成功');
                            router.replace('notic_list');
                            break;
                        default: tool.notif('error', '错误', msg);
                            break;
                    }
                })
            }
        },
        // 获取公告列表
        getList(con, e) {
            tool.request('/notice/getList', e).then(res => {
                let { code, msg, data, total } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveList', { data, total });
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },

        // 获取学生文件列表
        getFileList(con, e) {
            tool.request('/getStudentFileList', e).then(res => {
                let { code, msg, data, total } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveList', { data, total });
                        break;
                    default: tool.notif('error', '错误', msg);
                        break;
                }
            })
        },
        // 删除公告
        delNotice(con, e) {
            tool.confirm('提示', '此操作将永久删除该公告, 是否继续?').then(res => {
                if (res) {
                    tool.request('/notice/delNotice', { id: e.notice_id }).then(res => {
                        let { code, msg } = res.data;
                        switch (code) {
                            case 200:
                                tool.notif('success', '提示', '操作成功');
                                delete e.notice_id;
                                con.dispatch('getList', e);
                                break;
                            default: tool.notif('error', '错误', msg);
                                break;
                        }
                    })
                }
            })
        }
    },
    mutations: {
        SaveList(state, { data, total }) {
            state.noticeList = data;
            state.noticeTotal = total;
        },
        fileList(state, {data,total}){
            state.fileList = data;
            state.fileTotal = total;
        }
    },
    state: {
        noticeList: [],
        noticeTotal: 0,
        fileList: [],
        fileTotal: 0
    }
}

// 周报
const Report = {
    namespaced: true,
    actions: {
        // 获取学生周报
        getReportList(con,e) {
            tool.request('/report/getReportList', e).then(res => {
                let { code, msg, data, total } = res.data;
                switch (code) {
                    case 200:
                        con.commit('SaveReportList', data);
                        con.commit('SaveReportTotal', total);
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        },
        // 审核周报
        remark: (con,e) => new Promise((t,f) => {
            tool.request('/report/remark',e).then(res => {
                let { code, msg } = res.data;
                switch (code) {
                    case 200:
                        tool.notif('success','成功','批阅成功');
                        t();
                        break;
                    default: tool.notif('error', '错误', msg); break;
                }
            })
        })
    },
    mutations: {
        SaveReportList(state, e) {
            state.reportList = e;
        },
        SaveReportTotal(state,e) {
            state.reportTotal = e;
        },
    },
    state: {
        reportList: [],     // 学生周报列表
        reportTotal: 0
    }
}

// 模板
const EChdat = {
    namespaced: true,
    actions: {
        // 课题分类-课题总数
        topicTypeScale(con) {
            tool.request('/data/topicTypeScale').then(res => {
                let { name,total } = res.data;
                con.commit('SaveTopicTypeScale',{ name,total });
            })
        },
        // 职位-教师
        teacherStation(con) {
            tool.request('/data/teacherStation').then(res => {
                let { data } = res.data;
                con.commit('SaveTeacherStation',data);
            })
        },
        // 系部-班级-学生
        sdeptClassStudents(con) {
            tool.request('/data/sdeptClassStudents').then(res => {
                let { sdept,classTotal,studentTotal } = res.data;
                con.commit('SaveSdeptClassStudents',{ sdept,classTotal,studentTotal });
            })
        },
    },
    mutations: {
        SaveTopicTypeScale(state,e) {
            state.dataTT = e;
        },
        SaveTeacherStation(state,e) {
            state.dataTS = e;
        },
        SaveSdeptClassStudents(state,e) {
            state.dataCS = e;
        }
    },
    state: {
        dataTT: {},
        dataTS: [],
        dataCS: {}
    }
}

import axios from "axios";

const Dabian = {
    namespaced: true,
    actions: {
      releaseDabian(context,teamInf) {
        axios({
          method:"post",
          url:"http://localhost:8081/dabian/release",
          data:{
            teamInf
          }
        }).then(res=>{
          console.log(res)
        }).catch(ref => {
            f(ref)
        })
      }
    },
    mutations: {
       
    },
    state: {
      dabianTeam: []
    }
}

export default new Vuex.Store({
    modules: { User, Topic, Teacher, Student, Notice, Report, EChdat,Dabian }
})